﻿#include <QCoreApplication>
#include <math.h>
#include "SimulatedAnnualingSolver.h"

/* how many points do we try before stepping */
#define N_TRIES 100
/* Boltzmann constant */
/* initial temperature */
#define T_INITIAL 0.008
/* damping factor for temperature */
#define MU_T 1.003
#define T_MIN 2.0e-6

class SinModel : public SimulatedAnnualingSolver
{
public:
    SinModel();
protected:
    double take_step(double step_size) override;
    void undo_step() override;
    void save_best() override;
    double energy() override;
    void print() override;
private:
    std::mt19937 m_rand;
    double m_x;
    double m_best_x;
    double m_det_x;
};

SinModel::SinModel()
    :m_rand(1)
{
    m_x = 15.5;
}

double SinModel::energy()
{
    return exp(-pow((m_x - 1.0), 2.0)) * sin(8 * m_x);
}

void SinModel::save_best()
{
    m_best_x = m_x;
}

double SinModel::take_step(double step_size)
{
    step_size = 0;
    std::uniform_real_distribution<> dis(-1, 1);
    m_det_x = dis(m_rand);
    m_x += m_det_x;
    return energy();
}

void SinModel::undo_step()
{
    m_x -= m_det_x;
}

void SinModel::print()
{
    printf ("%12g", m_x);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    SinModel model;
    model.setParameters(T_INITIAL, 1.1);
    model.enablePrint(true);
    model.solve();
    return a.exec();
}
